package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.DiscountProductGiftCardDAO;
import com.ecoolex.discount.sales.core.service.YouWorldService;
import com.ecoolex.discount.sales.entity.*;
import com.ecoolex.discount.sales.enums.DiscountProductServiceType;
import com.ecoolex.discount.sales.pojo.EditDiscountProductGiftCardDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;

import static com.ecoolex.discount.sales.entity.DiscountProductGiftCard.Column;
import static com.ecoolex.discount.sales.entity.DiscountProductGiftCard.NOT_DELETED;


@Service
public class DiscountProductGiftCardManager {
    @Autowired
    private DiscountProductGiftCardDAO dao;

    @Autowired
    private YouWorldService youWorldService;
    @Autowired
    private DiscountProductInventoryManager discountProductInventoryManager;
    @Autowired
    private DiscountProductManager discountProductManager;

    void validityVerify(DiscountProduct product) {
        // 验证服务是否能支持
        if (DiscountProductServiceType.YOU_WORLD.equal(product.getServiceType())) {
            youWorldService.validityVerify(product);
        } else if (DiscountProductServiceType.INVENTORY.equal(product.getServiceType())) {
            discountProductInventoryManager.validityVerify(product);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "礼品卡折扣类型不支持此服务类型");
        }
    }

    void onlineVerify(DiscountProduct product) {
        BizAssert.isTrue(Check.notNull(product), "折扣产品不存在");
        DiscountProductGiftCard giftCard = getByProductId(product.getProductId());
        BizAssert.isTrue(Check.notNull(giftCard), "折扣礼品卡产品不存在");
        // 验证服务是否能支持
        if (DiscountProductServiceType.YOU_WORLD.equal(product.getServiceType())) {
            youWorldService.onlineVerify(product, giftCard);
        } else if (DiscountProductServiceType.INVENTORY.equal(product.getServiceType())) {
            discountProductInventoryManager.onlineVerify(product, giftCard);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "礼品卡折扣类型不支持此服务类型");
        }
    }


    void create(DiscountProduct product, String createUser) {
        DiscountProductGiftCard record = new DiscountProductGiftCard();
        record.setProductId(product.getProductId());
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    @Transactional
    public void update(Integer productId, EditDiscountProductGiftCardDTO edit, String updateUser) {
        DiscountProductGiftCard record = new DiscountProductGiftCard();
        record.setProductId(productId);
        record.setCurrencyType(edit.getCurrencyType());
        record.setCounterPriceType(edit.getCounterPriceType());
        record.setCounterPrice(edit.getCounterPrice());
        record.setRetailPrice(edit.getRetailPrice());
        record.setMaxBuyNumber(edit.getMaxBuyNumber());
        record.setMinCounterPrice(edit.getMinCounterPrice());
        record.setMaxCounterPrice(edit.getMaxCounterPrice());
        record.setCounterPriceDiscountPercent(edit.getCounterPriceDiscountPercent());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DiscountProductGiftCardExample example = new DiscountProductGiftCardExample();
        example.createCriteria()
                .andProductIdEqualTo(productId)
                .andDeletedEqualTo(NOT_DELETED);

        int row = dao.updateByExampleSelective(record,
                example,
                Column.currencyType,
                Column.counterPriceType,
                Column.maxBuyNumber,
                Column.counterPrice,
                Column.retailPrice,
                Column.minCounterPrice,
                Column.maxCounterPrice,
                Column.counterPriceDiscountPercent,
                Column.updateUser,
                Column.updateTime);

        BizAssert.isTrue(row == 1, "更新礼品卡信息失败");
        // 如果上线则需要做参数检查
        discountProductManager.checkIfOnlineAndVerify(productId);
    }

    public DiscountProductGiftCard getByProductId(Integer productId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(productId, NOT_DELETED);
    }

    boolean freeze(DiscountOrder order, String updateUser) {
        if (DiscountProductServiceType.INVENTORY.equal(order.getServiceType())) {
            return discountProductInventoryManager.freeze(order, updateUser);
        }
        return true;
    }

    void unfreeze(DiscountOrder order, String updateUser) {
        if (DiscountProductServiceType.INVENTORY.equal(order.getServiceType())) {
            discountProductInventoryManager.unfreeze(order, updateUser);
        }
    }

    public void processOrder(DiscountOrder order, String updateUser) {
        DiscountProductGiftCard giftCard = getByProductId(order.getProductId());
        BizAssert.isTrue(Check.notNull(giftCard), "ProductId={}, 礼品卡配置不存在", order.getProductId());

        if (DiscountProductServiceType.YOU_WORLD.equal(order.getServiceType())) {
            youWorldService.processOrder(order, giftCard, updateUser);
        } else if (DiscountProductServiceType.INVENTORY.equal(order.getServiceType())) {
            discountProductInventoryManager.processOrder(order, giftCard, updateUser);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "服务类型不支持");
        }
    }
    
    public void updatePercent(DiscountProductGiftCard record,String updateUser) {
    	record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.counterPriceDiscountPercent,Column.updateUser,Column.updateTime);
    }
}